﻿using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.PlanDetails;
using KDS.Mes.Mes.PlanDetails.Dtos;
using KDS.Mes.Web.Models.PlanDetails;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class PlanDetailsController : MesControllerBase
    {
        private readonly IPlanDetailAppService _planDetailAppService;

        public PlanDetailsController(IPlanDetailAppService planDetailAppService)
        {
            _planDetailAppService = planDetailAppService;
        }

        public ActionResult Index()
        {
            var model = new GetPlanDetailInput { FilterText = Request.QueryString["filterText"] };
            return View(model);
        }

        [DontWrapResult]
        public JsonResult GetAllPlanDetailsByPlanId(int limit, int offset, string sortfiled, string sortway,string planid)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetPlanDetailInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
                PlanId = int.Parse(planid)
            };
            var pagedTasks = _planDetailAppService.GetPagedPlanDetails(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_Plan_PlanDetails_Create, AppPermissions.Pages_Plan_PlanDetails_Edit)]
        public async Task<PartialViewResult> CreateOrEditPlanDetailModal(int? id)
        {
            var input = new NullableIdDto<int> { Id = id };

            var output = await _planDetailAppService.GetPlanDetailForEditAsync(input);

            var viewModel = new CreateOrEditPlanDetailModalViewModel(output);

            return PartialView("_CreateOrEditPlanDetailModal", viewModel);
        }
    }
}